#include"string.h" 
namespace bit
{
	string::string(const char* str)
	{
		_str = nullptr;
		_capacity = 0;
		_size = 0;
	}
	string::string(const string& s)
	{
		string tmp(s._str);
		swap(tmp);
	}
	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_capacity, s._capacity);
		std::swap(_size, s._size);
	}
	string& string::operator=(string& s)
	{
		swap(s);
		return *this;
	}
	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_capacity = 0;
		_size = 0;
	}

	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* tmp = new char[n+1];
			strcpy(tmp, _str);
			delete[] _str;
			_str = tmp;
			_capacity = n;
		}
	}

	void string::push_back(char c)
	{
		if (_size >= _capacity)
		{
			size_t NewCapacity = _capacity==0 ? 4 : _capacity*2;
			reserve(NewCapacity);
		}
		_str[_size] = c;
		_size++;
		_str[_size] = '\0';
	}
	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size+len >= _capacity)
		{
			size_t NewCapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(NewCapacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}
	string& string::operator+=(char c)
	{
		push_back(c);
		return *this;
	}
	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}
	void string::resize(size_t n, char c)
	{
		if (n < _size)
		{
			_str[n] = '\0';
			_size = n;
		}
		else if (n > _capacity)
		{
			reserve(n);
			for (size_t i = _size;i < n;i++)
			{
				_str[i] = '\0';
			}
			_size = n;
			_str[_size] = '\0';
		}
	}
	char& string::operator[](size_t index)
	{
		assert(index < _size);
		return *(_str + index);
	}
	const char& string::operator[](size_t index)const
	{
		assert(index < _size);
		return *(_str + index);
	}
	bool string::operator<(const string& s)
	{
		return strcmp(_str, s._str) < 0;
	}
	bool string::operator>(const string& s)
	{
		return strcmp(_str, s._str) > 0;
	}
	bool string::operator<=(const string& s)
	{
		return !(*this > s);
	}
	bool string::operator>=(const string& s)
	{
		return !(*this < s);
	}
	bool string::operator==(const string& s)
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator!=(const string& s)
	{
		return !(*this == s);
	}
	size_t string::find(char c, size_t pos) const
	{
		for (size_t i = pos;i < _size;i++)
		{
			if (_str[i] == c)
			{
				return i;
			}
		}
		return -1;
	}
	size_t string::find(const char* s, size_t pos) const
	{
		const char* ptr = strstr(_str + pos, s);
		if (ptr == nullptr)
		{
			return -1;
		}
		else
		{
			return ptr - _str;
		}
	}
	string& string::insert(size_t pos, char c)
	{
		assert(pos < _size);
		if (_size >= _capacity)
		{
			size_t NewCapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(NewCapacity);
		}
		for (size_t i = _size + 1;i > pos;i--)
		{
			_str[i] = _str[i-1];
		}
		_str[pos] = c;
		_size++;
		return *this;
	}
	string& string::insert(size_t pos, const char* str)
	{
		assert(pos < _size);
		size_t len = strlen(str);
		if (_size + len >= _capacity)
		{
			size_t NewCapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(NewCapacity);
		}
		for (size_t i = _size;i >= pos;i--)
		{
			_str[i+len] = _str[i];
		}
		strncpy(_str + pos, str, len);
		_size += len;
		return *this;
	}
	string& string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len == -1 || pos + len >= _size)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			for (size_t i = pos + len;i < _size;i++)
			{
				_str[i - len] = _str[i];
			}
			_size -= len;
		}
		return *this;
	}

	ostream& operator<<(ostream& _cout, const string& s)
	{
		for (auto ch : s)
		{
			_cout << ch;
		}
		return _cout;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();
		char buff[128];
		char ch = in.get();
		int i = 0;
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 127)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}

			ch = in.get();
		}

		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}

}
